<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Admin;

class LoginController extends Controller
{

  public function login(Request $request)
  {
    $username = $request->username ?: 1;
    $password = $request->password ?: '';
    $admin = Admin::where('name', $username)->where('status', 0)->first();
    if ($admin) {
      if (encrypt_password($password, $admin->salt) === $admin->password) {
        $last_login_time = millisecond();
        $token = md5($admin->name . $last_login_time); // token
        $admin->token = $token;
        $admin->last_login_time = $last_login_time;
        $admin->save();
        return resJson(self::HTTP_0_SUCCESS, ['token' => $token, 'id' => $admin->id]);
      }
    }
    return resJson(self::CUSTOM_10000_LOGIN_ERROR);
  }
}
